global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/Reduce_UK_minw.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {


*Our data on which we base our UK employment weighted minimum wages is part OF the NES survey by ONS.
* Bargained wages and employment by sector | NES (new earnings survey) | We thank Richard Dickens, Alan Manning and S.J, Machin
* The data came with restrictions and was tehrefore not shared in full but rather reduced to the lowest number of variables possible. This is the code that documents it.
*We only share the resulting data from this file, hence the check for some variables in the beginning that were dropped in case someone obtains the full data and wants to replicate the process.
* Bargained wages and employment by sector | NES (new earnings survey) | We thank Richard Dickens, Alan Manning and S.J, Machin 
use ${mow_data_raw}/Minimum_wages/nes_eg_data7594.dta, clear

*Check for a variable that is not in the data after this file has been run. 
cap ds ftf
if _rc!=0 {
    display "File has alread been run and unnecessary data already discarded. No need to rerun."
}
else {
    *keep only needed variables
    keep year wbc empall_male empall_fem minw 

    * keep only the industry codes that we use in the analysis
    * Let's settle on the manufacturing production. 
    * clothing == 3
    * toy manufacturing == 11 

    local wbc_keep " 3 11 "
    gen mgo=.
    foreach wbc_run in `wbc_keep' {
        replace mgo=1 if wbc==`wbc_run'
    }
    keep if mgo==1
    drop mgo

    *Add up male and female employment to total employment. 
    gen emp_tot=empall_male+empall_fem
    *Calculate the mean of the minimum wage weighted by employment for each year.
    collapse minw [w=emp_tot], by(year)
    rename minw min_wage
    gen country="United Kingdom"

    *This is the resulting dataset that we share. We only share this to avoid privacy issues with the original data.
    save ${mow_data_raw}/Minimum_wages/nes_eg_data7594.dta, replace
}

}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat